﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//This Class specific System namespaces
using System.Data;
using System.Data.Common;

//This Class Specific IvyFramework namespaces
using Ivy.Common.Framework.Base;
using Ivy.Common.Framework.DataAccess;

using Ivy.SiteAdministration.Entity;

namespace Ivy.SiteAdministration.DataAccess
{
    public class Sys_SessionLogReportDao
    {
        #region  Variable & constant
        // All constants and local variable should be declared here
        // TRY NOT TO CREATE PUBLIC VARIABLES INSTEAD USE PROPERTIES FOR THAT PURPOSE.
        // Variable naming convention :- 
        // The naming should be self explanatory, make it long to explain the purpose 
        // and use PascalCase to seperate words as shown in the example below.
        // DO NOT USE ANY ABBRIVIATION use full word (like "Customer" instead of "Cust"
        // Naming convention for all local variable should start with underscore "_"
        // Naming convention for all local constant should start with "_The"
        // Example :    string _CustomerCode = string.Empty;
        // Example :    const string _TheDefaultSmtpServer = "DefaultSmtpServer";
        // ==================== START YOUR CODE FROM NEXT LINE ========================


        #endregion  Variable & constant

        #region Constructor & Destructor
        // Cut and Copy the default constructor for the class inside this region.
        // Generally not required in UI layer.
        // Example:
        // public SampleClassTemplate()
        // {

        // }
        // ==================== START YOUR CODE FROM NEXT LINE ========================


        #endregion Constructor & Destructor

        #region Properties
        // Any value/variable that needs to be exposed outside the class we need to create a property.
        // Avoid creating public variables rather create public properties.
        // UI layer may have public custom properties. these are very handy for User Controls.
        // Property naming convention :- Properties will be exposed outside the class, 
        // the naming should be self explanatory, make it long to explain the purpose 
        // and use PascalCase to seperate words as shown in the example below.
        // DO NOT USE ANY ABBRIVIATION use full word (like "Customer" instead of "Cust"
        // All properties should have a corresponding local variable 
        // and the property name and local variable name should match.
        // Example :
        // public string CustomerCode
        // {
        //     get
        //     {
        //         return _CustomerCode;
        //     }
        //     set
        //     {
        //         _CustomerCode = value;
        //     }
        // }
        // ==================== START YOUR CODE FROM NEXT LINE ========================


        #endregion Properties

        #region Events
        // Generally UI layer will have public events.
        // Cut and Copy the default generated events for the class inside this region.
        // ==================== START YOUR CODE FROM NEXT LINE ========================


        #endregion Events

        #region Public Methods
        // Any method that needs to be exposed outside the class will be created inside this region.
        // Public Method naming convention :- Public Method will be exposed outside the class, 
        // The input and output parameters are also going to be exposed outside the class
        // the naming should be self explanatory, make it long to explain the purpose 
        // and use PascalCase to seperate words as shown in the example below.
        // DO NOT USE ANY ABBRIVIATION use full word (like "Customer" instead of "Cust"
        // Example :
        // public bool RegisterNewCustomer(Customer Customer)
        // {
        //     bool IsCustomerDataSaved = false;
        //     //TO DO logic to save data
        //     IsCustomerDataSaved = InsertCustomerDataUsingDao(Customer);
        //     return IsCustomerDataSaved;
        // }
        // ==================== START YOUR CODE FROM NEXT LINE ========================
        public List<Sys_SessionLogReport> GetAll()
        {
            List<Sys_SessionLogReport> lSys_SessionLogReportList = new List<Sys_SessionLogReport>();

            DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
            DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
            CmdObj.CommandText = "Ivy_Sys_SessionLogReport_GetAll";
            CmdObj.CommandType = CommandType.StoredProcedure;

            CmdObj.Connection = DbConn;
            DbConn.Open();
            CmdObj.Prepare();

            DbDataReader _DataReader = CmdObj.ExecuteReader();
            if (_DataReader.HasRows)
            {
                while (_DataReader.Read())
                {
                    var lSys_SessionLogReporttemp = new Sys_SessionLogReport(); //IMPORTANT NOTE:- Create instance only when required.
                    this.TranslateUserFieldsFromReaderToEntity(lSys_SessionLogReporttemp, _DataReader);
                    lSys_SessionLogReportList.Add(lSys_SessionLogReporttemp);
                }
            }
            CmdObj.Dispose();
            DbConn.Close();


            return lSys_SessionLogReportList;
        }

        public Sys_SessionLogReport GetByKey(int SessionLogPrimaryID)
        {
            Sys_SessionLogReport _SelectedSys_SessionLogReport = null;

            DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
            DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
            CmdObj.CommandText = "Ivy_Sys_SessionLogReport_GetByKey";
            CmdObj.CommandType = CommandType.StoredProcedure;

            DataAccessProviderUtility.AddParameter(CmdObj, "@SessionLogPrimaryID", ParameterDirection.Input, DbType.Int32, (SessionLogPrimaryID));
            CmdObj.Connection = DbConn;
            DbConn.Open();
            CmdObj.Prepare();
            DbDataReader _DataReader = CmdObj.ExecuteReader();

            if (_DataReader.HasRows)
            {
                if (_DataReader.Read())
                {
                    _SelectedSys_SessionLogReport = new Sys_SessionLogReport();
                    TranslateUserFieldsFromReaderToEntity(_SelectedSys_SessionLogReport, _DataReader);
                }
            }

            return _SelectedSys_SessionLogReport;

        }

        #endregion Public Methods

        #region Private Methods
        // All local class level methods will be inside this region. Mostly these are helper methods.
        // Private Method naming convention :- The naming should be self explanatory, 
        // make it long to explain the purpose and use PascalCase to seperate words as shown in the example below.
        // DO NOT USE ANY ABBRIVIATION use full word (like "Customer" instead of "Cust"
        // Input parameter will have a "p" as prefix. 
        // This will help the parameter variable that the value is originated outside this private method.
        // Example :
        // public bool InsertCustomerDataUsingDao(Customer pCustomer)
        // {
        //     bool IsCustomerDataInsertedSuccessfully = false;
        //     //TO DO logic to save data
        //     //....
        //     //....
        //     return IsCustomerDataInsertedSuccessfully;
        // }
        // ==================== START YOUR CODE FROM NEXT LINE ========================

        private void TranslateUserFieldsFromReaderToEntity(Sys_SessionLogReport pSys_SessionLogReport, DbDataReader pDbDataReader)
        {
            pSys_SessionLogReport.SessionLogPrimaryID = Convert.ToInt32(pDbDataReader["SessionLogPrimaryID"]);
            pSys_SessionLogReport.UserSessionID = Convert.ToString(pDbDataReader["UserSessionID"]);
            pSys_SessionLogReport.UserIPAddress = Convert.ToString(pDbDataReader["UserIPAddress"]);
            pSys_SessionLogReport.UserSessionStartTime = Convert.ToDateTime(pDbDataReader["UserSessionStartTime"]);

            if (!pDbDataReader.IsDBNull(pDbDataReader.GetOrdinal("UserSessionEndTime")))
            {
                pSys_SessionLogReport.UserSessionEndTime = Convert.ToDateTime(pDbDataReader["UserSessionEndTime"]);
            }
            else
            {
                pSys_SessionLogReport.UserSessionEndTime = null;
            }
            pSys_SessionLogReport.UserBrowserName = Convert.ToString(pDbDataReader["UserBrowserName"]);
            pSys_SessionLogReport.PersonFirstName = Convert.ToString(pDbDataReader["PersonFirstName"]);
            pSys_SessionLogReport.PersonLastName = Convert.ToString(pDbDataReader["PersonLastName"]);
            pSys_SessionLogReport.PersonPrimaryEmailID = Convert.ToString(pDbDataReader["PersonPrimaryEmailID"]);
            pSys_SessionLogReport.OrganizationName = Convert.ToString(pDbDataReader["OrganizationName"]);

        }
        #endregion Private Methods
    }
}
